import { Component } from '@angular/core';
import {
  IonicPage,
  NavController,
  NavParams,
  ToastController
} from 'ionic-angular';
import { BaseUI } from '../../../common/baseUI';

/**
 * Generated class for the ForgetPasswordPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-forget-password',
  templateUrl: 'forget-password.html'
})
export class ForgetPasswordPage extends BaseUI {
  password: string = '';
  repassword: string = '';
  isShow: Boolean = true; //判断是否显示倒计时
  count: number = 60; // 倒计时时间
  timer: any = null; // 定时器的名称
  tel: string = ''; // 双向绑定手机号码
  code: number; // 验证码
  radom = Math.floor(Math.random() * 100);
  isEye: boolean = true;

  constructor(
    public navCtrl: NavController,
    public navParams: NavParams,
    public toastCtrl: ToastController
  ) {
    super();
  }

  ionViewDidLoad() {}

  gotoUpDataInfo() {
    const reg = /^1[34578][0-9]{9}$/;
    if (!reg.test(this.tel)) {
      this.showToast(this.toastCtrl, '手机格式不正确');
    } else if (this.code !== 0) {
      this.showToast(this.toastCtrl, '请检查验证码');
    } else if (
      this.password.length >= 8 &&
      this.password.length <= 16 &&
      this.repassword === this.password
    ) {
      this.showToast(this.toastCtrl, '修改成功');
      this.navCtrl.popToRoot();
    } else if (this.password.length < 8) {
      this.showToast(this.toastCtrl, '密码长度需要大于8位');
    } else if (this.password.length > 16) {
      this.showToast(this.toastCtrl, '密码长度需要小于16位');
    } else if (this.password !== this.repassword) {
      this.showToast(this.toastCtrl, '密码与确认密码不符');
    }
  }

  setCode() {
    const reg = /^1[34578][0-9]{9}$/;
    if (reg.test(this.tel)) {
      this.isShow = false;
      this.setTiem();
    } else {
      this.showToast(this.toastCtrl, '手机格式不正确');
    }
  }

  // 设计定时器
  setTiem() {
    clearInterval(this.timer);
    this.timer = setInterval(() => {
      this.count--;
      if (this.count === 0) {
        this.count = 60;
        this.isShow = true;
        clearInterval(this.timer);
      }
    }, 1000);
  }

  blurInput() {
    const reg = /^1[34578][0-9]{9}$/;
    if (!reg.test(this.tel)) {
      this.showToast(this.toastCtrl, '手机格式不正确');
    } else if (1) {
      // TODO: 请求接口 判断是否注册
    }
  }
}
